package lib;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import com.dy.tieba.service.PostService;
import com.dy.tieba.service.UserRobotService;
import com.dy.video.service.Transfer;
import com.google.inject.Guice;
import com.google.inject.Inject;

import inject.DefaultModule;
public class BasicService {

	@Inject
	public static PostService postService;
	
	@Inject
	public static Transfer transfer;
	@Inject
	public static UserRobotService userRobotService;
	
	
	static {
		try {
			//静态注入
			controllerInject();
			System.out.println("静态注入");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	static void controllerInject() throws Exception{
        Field[] fields = BasicService.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Inject.class) && Modifier.isStatic(field.getModifiers())) {
                field.setAccessible(true);//setAccessible(true)后 性能有了20倍的提升,功能是启用或禁用安全检查
                Class<?> fieldClazz = field.getType();
                Object fieldObj = Guice.createInjector(new DefaultModule(fieldClazz)).getInstance(fieldClazz);
                field.set(null, fieldObj);
            }
        }
}
}
